<?php
	$title='Fitness forum';
	$description='Fitness forum';
	$keywords='fitness, muscles, training, supplements, diet, nutririon, forum';
?>
<?php include('fns_db_forum.php'); ?>
<?php include('../views/layouts/_header.php'); ?>
<?php include('../views/layouts/_left.php'); ?>

<?php
$result = all_forums();
$isAdmin = checkIfAdmin();

if ($result == 0)
{
	echo "<b>Error ";
	echo mysqli_errno($cxn).": ".mysqli_error($cxn);
	echo "</b>";
}
else
{
	echo "<table width=\"100%\"
	class=\"forumline\"cellpadding=\"4\"";
	echo " cellspacing=\"2\" border=\"0\" align=\"center\">";
	echo "<tbody>";
	echo '<tr class="heading2">
		<th colspan="2" nowrap="nowrap">Forum</th>
		<th nowrap="nowrap">Topic</th>
		<th nowrap="nowrap">Threads</th>
		<th nowrap="nowrap">Description</th>
		</tr>';

	for( $i = 0; $i < mysqli_num_rows( $result ); $i++ )
	{
		$forum = mysqli_fetch_assoc( $result );
		echo "<tr>";
			echo ' <td class="heading3" colspan="5">'.$forum['name'];
				if($isAdmin){
					echo '<br/><a href="delete.php?target=forum&id=' .$forum['id'] . '">delete</a>';
				}
			echo '</td>';
		echo "</tr>\n";

		$result2 = all_topics_for_forum($forum['id']);
		for( $j = 0; $j < mysqli_num_rows( $result2 ); $j++ )
		{
			echo '<tr>';
			$topic = mysqli_fetch_assoc( $result2 );
			echo '<td class="topicFiller" colspan="2">&nbsp;</td>';
			echo '<td class="topicLink">';
				echo '<a href="view_topic.php?topicID=' .$topic['id'] . '">';
				echo $topic['name'];
				echo '</a>';
				if($isAdmin){
					echo '&nbsp;<a href="delete.php?target=topic&id=' .$topic['id'] . '">delete</a>';
				}
			echo '</td>';
			echo '<td>';
			echo get_threads_count_for_topic($topic['id']);
			echo '</td>';
			echo '<td class=topicDesc>'.
			$topic['description']. '</td>';
			echo "</tr>\n";
		}
	}
	echo "</tbody></table>";

	if($isAdmin){
		print('<br/><a href="add_topic.php">add new topic</a>');
		print('<br/><a href="add_forum.php">add new forum</a>');
	}
}
?>

<?php include('../views/layouts/_footer.php'); ?>